﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace Enterprise.Web
{
    public class Pages
    {
        /// <summary>
        /// 获取上一页页码
        /// </summary>
        /// <param name="current">当前页</param>
        /// <returns></returns>
        public int GetPagePrev(int current, int pages)
        {
            if (current > 1 && current < pages)
                return current - 1;
            else if (current > pages)
                return pages - 1;
            else
                return 1;
        }


        /// <summary>
        /// 获取下一页页码
        /// </summary>
        /// <param name="current">当前页</param>
        /// <param name="pages">总页数</param>
        /// <returns></returns>
        public int GetPageNext(int current, int pages)
        {
            if (current < pages)
                return current + 1;
            else
                return pages;
        }

        /// <summary>
        /// 分页条显示开始页
        /// </summary>
        /// <param name="current">当前页</param>
        /// <param name="pages">总页数</param>
        /// <param name="pagenumbers">显示几页</param>
        /// <returns></returns>
        public int GetPageFirst(int current, int pages, int pagenumbers)
        {
            int mid = pagenumbers / 2;
            if (pages <= pagenumbers)
                return 1;
            else
            {
                int first = (current - mid) > 1 ? (current - mid) : 1;
                return first > (pages - pagenumbers + 1) ? (pages - pagenumbers + 1) : first;
            }
        }

        /// <summary>
        /// 分页条显示结束页
        /// </summary>
        /// <param name="current">当前页</param>
        /// <param name="pages">总页数</param>
        /// <param name="pagenumbers">显示几页</param>
        /// <returns></returns>
        public int GetPageLast(int current, int pages, int pagenumbers)
        {
            int mid = pagenumbers / 2;
            if (pages <= pagenumbers)
                return pages;
            else
            {
                int last = ((current + mid) < pagenumbers) ? pagenumbers : (current + mid);
                return last < pages ? last : pages;
            }
        }

        /// <summary>
        /// 设置分页参数
        /// </summary>
        public void SetPage(VelocityHelper vh, int totalCount,int totalPage, int pageIndex,int pageSize)
        {
            vh.Put("currentPage", pageIndex);//当前页码
            vh.Put("pageSize", pageSize);//每页显示数目
            vh.Put("totalCount", totalCount);//获取记录数
            vh.Put("totalPage", totalPage);//总页数
            vh.Put("pager", new Pages());
        }
    }
}
